package com.open.capacity.form.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;
import com.open.capacity.commons.PageResult;
import com.open.capacity.db.config.util.DataSourceHolder;
import com.open.capacity.db.config.util.DataSourceKey;
import com.open.capacity.form.dao.FormDbTableDao;
import com.open.capacity.form.model.FormSchemeinfo;
import com.open.capacity.form.service.FormDbTableService;
import com.open.capacity.form.vo.FormDbTableFieldVo;
import com.open.capacity.form.vo.FormDbTableVo;
import com.open.capacity.utils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class FormDbTableServiceImpl implements FormDbTableService {


    @Autowired
    private FormDbTableDao dbTableDao;


    @Override
    public List<FormDbTableFieldVo> findFieldsByTableName(String dbKey, String tableName) {
        try {
            DataSourceKey dataSourceKey = DataSourceKey.valueOf(dbKey);
            DataSourceHolder.setDataSourceKey(dataSourceKey);
        } catch (Exception e) {
            log.error("数据源[{}]不存在，使用默认数据源 > {}", dbKey, DataSourceHolder.getDataSourceKey());
        }
        List<FormDbTableFieldVo> list = dbTableDao.findFieldsByTableName(tableName);
        log.debug("Revert DataSource : {} > {}", dbKey, DataSourceHolder.getDataSourceKey());
        DataSourceHolder.clearDataSourceKey();
        return list;
    }

    @Override
    public List<FormDbTableVo> findList(String dbKey, String tableName) {
        try {
            DataSourceKey dataSourceKey = DataSourceKey.valueOf(dbKey);
            DataSourceHolder.setDataSourceKey(dataSourceKey);
        } catch (Exception e) {
            log.error("数据源[{}]不存在，使用默认数据源 > {}", dbKey, DataSourceHolder.getDataSourceKey());
        }
        List<FormDbTableVo> list = dbTableDao.findList(tableName);
        log.debug("Revert DataSource : {} > {}", dbKey, DataSourceHolder.getDataSourceKey());
        DataSourceHolder.clearDataSourceKey();
        return list;
    }

    @Override
    public List<Map<String, Object>> findData(String dbKey, String sql, Map<String, Object> params) {
        try {
            DataSourceKey dataSourceKey = DataSourceKey.valueOf(dbKey);
            DataSourceHolder.setDataSourceKey(dataSourceKey);
        } catch (Exception e) {
            log.error("数据源[{}]不存在，使用默认数据源 > {}", dbKey, DataSourceHolder.getDataSourceKey());
        }
        params.put("sql", sql);
        List<Map<String, Object>> list = dbTableDao.selectList(params);
        log.debug("Revert DataSource : {} > {}", dbKey, DataSourceHolder.getDataSourceKey());
        DataSourceHolder.clearDataSourceKey();
        return list;
    }

    @Override
    public PageResult<Map<String, Object>> pageData(String dbKey, String sql, Map<String, Object> params) {
        try {
            DataSourceKey dataSourceKey = DataSourceKey.valueOf(dbKey);
            DataSourceHolder.setDataSourceKey(dataSourceKey);
        } catch (Exception e) {
            log.error("数据源[{}]不存在，使用默认数据源 > {}", dbKey, DataSourceHolder.getDataSourceKey());
        }
        params.put("sql", sql);

        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<Map<String, Object>>  list = dbTableDao.pageList(params);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo(list);
        log.debug("Revert DataSource : {} > {}", dbKey, DataSourceHolder.getDataSourceKey());
        DataSourceHolder.clearDataSourceKey();
        return PageResult.<Map<String, Object>>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();

    }


    @Override
    public Integer excuteSql(String dbKey, String sql, String whereStr) {
        try {
            DataSourceKey dataSourceKey = DataSourceKey.valueOf(dbKey);
            DataSourceHolder.setDataSourceKey(dataSourceKey);
        } catch (Exception e) {
            log.error("数据源[{}]不存在，使用默认数据源 > {}", dbKey, DataSourceHolder.getDataSourceKey());
        }
        Integer res = dbTableDao.excuteSql(sql + " " + whereStr);
        log.debug("Revert DataSource : {} > {}", dbKey, DataSourceHolder.getDataSourceKey());
        DataSourceHolder.clearDataSourceKey();
        return res;
    }

    @Override
    public Integer excuteSqlWithParams(String dbKey, String sql, Map<String, Object> params) {
        try {
            DataSourceKey dataSourceKey = DataSourceKey.valueOf(dbKey);
            DataSourceHolder.setDataSourceKey(dataSourceKey);
        } catch (Exception e) {
            log.error("数据源[{}]不存在，使用默认数据源 > {}", dbKey, DataSourceHolder.getDataSourceKey());
        }
        params.put("sql", sql);
        Integer res = dbTableDao.excuteSqlWithParams(params);
        log.debug("Revert DataSource : {} > {}", dbKey, DataSourceHolder.getDataSourceKey());
        DataSourceHolder.clearDataSourceKey();
        return res;
    }
}
